package com.ruoyi.project.zerocarbon.controller;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.mapper.SysUserMapper;
import com.ruoyi.project.zerocarbon.domain.Declaration;
import com.ruoyi.project.zerocarbon.domain.DeclareExpert;
import com.ruoyi.project.zerocarbon.domain.vo.DeclareExpertGroupVo;
import com.ruoyi.project.zerocarbon.mapper.DeclarationMapper;
import com.ruoyi.project.zerocarbon.mapper.DeclareExpertMapper;
import com.ruoyi.project.zerocarbon.service.IDeclarationService;
import com.ruoyi.project.zerocarbon.service.IDeclareExpertService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2021-07-31
 */
@Api("小组选择相关")
@RestController
@RequestMapping("/zerocarbon/expert")
public class DeclareExpertController extends BaseController
{
    @Autowired
    private DeclarationMapper declarationMapper;
    @Autowired
    private IDeclarationService declarationService;
    @Autowired
    private IDeclareExpertService declareExpertService;
    @Autowired
    private DeclareExpertMapper declareExpertMapper;
    @Autowired
    private SysUserMapper userMapper;

    @ApiOperation("小组选择列表")
    @GetMapping("/groupList")
    public AjaxResult groupList(Integer managementType)
    {
        Assert.notNull(managementType);
        List<DeclareExpertGroupVo> list = new ArrayList<>();
        if (managementType == 1){
            String[] group1Names = {
                    "项目第1组",
                    "项目第2组",
                    "项目第3组",
                    "项目第4组",
                    "项目第5组",
                    "项目第6组",
                    "项目第7组",
                    "项目第8组",
                    "项目第9组",
                    "项目第10组",
                    "项目第11组",
                    "项目第12组",
                    "项目第13组",
                    "项目第14组",
                    "项目第15组",
                    "项目第16组",
                    "项目第17组",
                    "项目第18组",
                    "项目第19组",
                    "项目第20组"
            };
            for (int i = 0; i < group1Names.length; i++) {
                int count = declareExpertMapper.countExpertByGroupName(group1Names[i]);
                DeclareExpertGroupVo vo = new DeclareExpertGroupVo();
                vo.setGroupName(group1Names[i]);
                vo.setNum(count);
                list.add(vo);
            }
        }else if (managementType == 2){
            String[] group2Names = {
                    "学术论著第1组",
                    "学术论著第2组",
                    "学术论著第3组",
                    "学术论著第4组",
                    "学术论著第5组"
            };
            for (int i = 0; i < group2Names.length; i++) {
                int count = declareExpertMapper.countExpertByGroupName(group2Names[i]);
                DeclareExpertGroupVo vo = new DeclareExpertGroupVo();
                vo.setGroupName(group2Names[i]);
                vo.setNum(count);
                list.add(vo);
            }
        }else{
            String[] group3Names = {
                    "创意作品第1组",
                    "创意作品第2组",
                    "创意作品第3组",
                    "创意作品第4组",
                    "创意作品第5组",
                    "创意作品第6组"
            };
            for (int i = 0; i < group3Names.length; i++) {
                int count = declareExpertMapper.countExpertByGroupName(group3Names[i]);
                DeclareExpertGroupVo vo = new DeclareExpertGroupVo();
                vo.setGroupName(group3Names[i]);
                vo.setNum(count);
                list.add(vo);
            }
        }

        return AjaxResult.success(list);
    }

    @ApiOperation("小组项目列表")
    @GetMapping("/group/management/view")
    public AjaxResult groupView(String groupName)
    {
        Assert.notNull(groupName);
        DeclareExpert declareExpert = new DeclareExpert();
        declareExpert.setGroupName(groupName);
        List<DeclareExpert> declareExperts = declareExpertMapper.selectDeclareExpertList(declareExpert);
        List<Declaration> declarations = new ArrayList<>();
        if (CollectionUtils.isEmpty(declareExperts)){
            return AjaxResult.success(declarations);
        }
        List<Long> longList = declareExperts.stream().map(model -> model.getDeclarationId()).collect(Collectors.toList());
        for (Long declarationId:longList) {
            Declaration declaration = declarationMapper.selectDeclarationById(declarationId);
            declarations.add(declaration);
        }
        return AjaxResult.success(declarations);
    }


    /**
     * 新增【请填写功能名称】
     */
    @ApiOperation("选择专家，declarationIdList和groupName")
    @PostMapping("/decoration/expert/add")
    public AjaxResult selectExpert(@RequestBody DeclareExpert declareExpert)
    {
        Assert.notNull(declareExpert.getGroupName());
        if (CollectionUtils.isNotEmpty(declareExpert.getDeclarationIdList())){
            for (int i = 0; i < declareExpert.getDeclarationIdList().size(); i++) {
                DeclareExpert modelE = declareExpertMapper.selectDeclareExpertByDeclarationId(declareExpert.getDeclarationIdList().get(i));
                if (modelE != null){
                    throw new CustomException("不能重复分配");
                }
                modelE = new DeclareExpert();
                BeanUtils.copyProperties(declareExpert,modelE);
                modelE.setDeclarationId(declareExpert.getDeclarationIdList().get(i));
                int i1 = declareExpertService.insertDeclareExpert(modelE);
                Declaration declaration = new Declaration();
                declaration.setId(declareExpert.getDeclarationIdList().get(i));
                declaration.setDistributionStatus(1);
                declaration.setUpdateTime(new Date());
                declarationService.updateDeclaration(declaration);
            }
        }
        return AjaxResult.success();
    }

    @ApiOperation("查看(根据组别)group=项目第1组，group对应user中的remark")
    @GetMapping("/viewByGroup")
    public AjaxResult getInfo(String group)
    {
        Assert.notNull(group);
        List<SysUser> sysUsers = userMapper.selectUserByRemark(group);
        return AjaxResult.success(sysUsers);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('zerocarbon:expert:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeclareExpert declareExpert)
    {
        startPage();
        List<DeclareExpert> list = declareExpertService.selectDeclareExpertList(declareExpert);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('zerocarbon:expert:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DeclareExpert declareExpert)
    {
        List<DeclareExpert> list = declareExpertService.selectDeclareExpertList(declareExpert);
        ExcelUtil<DeclareExpert> util = new ExcelUtil<DeclareExpert>(DeclareExpert.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('zerocarbon:expert:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(declareExpertService.selectDeclareExpertById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('zerocarbon:expert:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeclareExpert declareExpert)
    {
        return toAjax(declareExpertService.insertDeclareExpert(declareExpert));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('zerocarbon:expert:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeclareExpert declareExpert)
    {
        return toAjax(declareExpertService.updateDeclareExpert(declareExpert));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('zerocarbon:expert:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(declareExpertService.deleteDeclareExpertByIds(ids));
    }
}
